package com.igd.action.develop;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.DocumentException;

import com.igd.action.BaseAction;
import com.igd.exceptions.IgdException;
import com.igd.pojo.vo.ExtData;
import com.igd.utils.dev.DbMapperUtil;

 /**
 * @version  1.0
 *
 * @author   QIAOYU
 *
 * @date     2013-7-8
 *
 * @description	 数据库表反转Java对象与SQLMAP文件
 */

public class DbMapperAction extends BaseAction {

	private static final long serialVersionUID = 3438128684101947144L;
	
	private DbMapperUtil du = DbMapperUtil.getInstance();
	
	/**数据库驱动名称*/
	private String driverName;
	
	/**数据库连接字符串*/
	private String url;
	
	/**数据库用户名*/
	private String userName;
	
	/**数据库密码*/
	private String passWord;
	
	/**映射的数据库表名*/
	private String tableName;
	
	/**数据库schema,Oracle使用schema,Mysql使用catalog,此处为了程序统一使用schema*/
	private String schema;
	
	/**
	 * 跳转到反转页面
	 * 
	 * @return
	 */
	public String toMapper(){
		
		return SUCCESS;
	}
	
	/**
	 * 获取数据库表名
	 * 
	 * @return String
	 * 
	 * @throws SQLException
	 * 
	 * @throws ClassNotFoundException
	 * 
	 * @throws IgdException 
	 */
	public String getDataBaseTables() throws SQLException, ClassNotFoundException, IgdException{
		
		List<String> tableNames = du.getTables(du.getDbConnection(this.getDriverName(),this.getUrl(),this.getUserName(),this.getPassWord()),this.getSchema());
		
		ExtData<String> extData = new ExtData<String>();
		
		if(null != tableNames && tableNames.size() > 0){
			
			extData.setRspData(tableNames);
			
		}else{
			
			extData.setStatusCode(ExtData.FAIL);
			
			throw new IgdException("dbmapper.getTablesError");

		}
		
		this.putExtData(extData);
		
		return this.JSON_PAGE;
	}
	
	/**
	 * 获取根据表名产生的JavaBean代码以及ibatis sqlmap文件
	 * 
	 * @return String
	 * 
	 * @throws ClassNotFoundException
	 * 
	 * @throws SQLException
	 * 
	 * @throws DocumentException 
	 * 
	 * @throws IgdException 
	 */
	public String getMapperCode() throws ClassNotFoundException, SQLException, DocumentException, IgdException{
		
		Connection conn = du.getDbConnection(this.getDriverName(),this.getUrl(),this.getUserName(),this.getPassWord());
		
		ExtData<String> extData = new ExtData<String>();
		
		if(null != conn){
			
			/**因为只返回JavaBean代码与ibatis sqlmap代码所以只初始化2个长度的list*/
			List<String> mapperCodeList = new ArrayList<String>(2);
			
			String javaCodeStr = du.mapperJavaCode(conn,this.getTableName(),this.getSchema());
			
			String ibaitsSqlMapStr = du.mapperIbatisCode(conn,this.getTableName(),this.getSchema());
			
			mapperCodeList.add(javaCodeStr);

			mapperCodeList.add(ibaitsSqlMapStr);
			
			extData.setRspData(mapperCodeList);
			
		}else{
			
			extData.setStatusCode(ExtData.FAIL);
			
		}
		
		this.putExtData(extData);
		
		return this.JSON_PAGE;
	}
	
	public String getDriverName() {
		return driverName;
	}

	public void setDriverName(String driverName) {
		this.driverName = driverName;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassWord() {
		return passWord;
	}

	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getSchema() {
		return schema;
	}

	public void setSchema(String schema) {
		this.schema = schema;
	}
}
